import Skeleton, { SkeletonTheme } from "react-loading-skeleton";
import "react-loading-skeleton/dist/skeleton.css";
import { ClockIcon, HeartIcon } from "@heroicons/react/20/solid";
import {
TvIcon,
ArrowTrendingUpIcon,
RectangleStackIcon,
} from "@heroicons/react/24/outline";
import Head from "next/head";
import Image from "next/image";
import { useRouter } from "next/router";
import { useEffect, useState } from "react";
import Layout from "../../components/layout";
import Link from "next/link";
import Content from "../../components/hero/content";
import Modal from "../../components/modal";
import { signIn, useSession } from "next-auth/react";
import AniList from "../../components/media/aniList";
import ListEditor from "../../components/listEditor";
import { GET_MEDIA_USER } from "../../queries";
import { GET_MEDIA_INFO } from "../../queries";
// import { aniInfo } from "../../components/devComp/data";
// console.log(GET_MEDIA_USER);
export default function Info({ info, color }) {
const { data: session } = useSession();
const [episode, setEpisode] = useState(null);
const [loading, setLoading] = useState(false);
const [progress, setProgress] = useState(0);
const [statuses, setStatuses] = useState(null);
const [domainUrl, setDomainUrl] = useState("");
const [showAll, setShowAll] = useState(false);
const [open, setOpen] = useState(false);
const [time, setTime] = useState(0);
const { id } = useRouter().query;
const [epiView, setEpiView] = useState("3");
const [artStorage, setArtStorage] = useState(null);
const rec = info?.recommendations?.nodes?.map(
(data) => data.mediaRecommendation
);
useEffect(() => {
handleClose();
async function fetchData() {
setLoading(true);
if (id) {
try {
const { protocol, host } = window.location;
const url = `${protocol}//${host}`;
const view = localStorage.getItem("epiView");
setDomainUrl(url);
setArtStorage(JSON.parse(localStorage.getItem("artplayer_settings")));
setEpisode(null);
setProgress(0);
setStatuses(null);
const res = await fetch(
`https://api.moopa.my.id/meta/anilist/info/${info.id}`
);
// const res = true;
if (res.status === 500 || res.status === 404) {
// if (res === false) {
setEpisode([]);
} else {
const data = await res.json();
// const data = aniInfo;
if (!data || data?.episodes?.length === 0) {
setEpisode([]);
} else {
if (data.episodes?.some((i) => i.title === null)) {
setEpiView("3");
} else if (view) {
setEpiView(view);
} else {
setEpiView("3");
}
setEpisode(data?.episodes.reverse());
}
if (session?.user?.name) {
const response = await fetch("https://graphql.anilist.co/", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
query: GET_MEDIA_USER,
variables: {
username: session?.user?.name,
},
}),
});
const responseData = await response.json();
const prog = responseData?.data?.MediaListCollection;
if (prog && prog.lists.length > 0) {
const gut = prog.lists
.flatMap((item) => item.entries)
.find((item) => item.mediaId === parseInt(id[0]));
if (gut) {
setProgress(gut.progress);
const statusMapping = {
CURRENT: { name: "Watching", value: "CURRENT" },
PLANNING: { name: "Plan to watch", value: "PLANNING" },
COMPLETED: { name: "Completed", value: "COMPLETED" },
DROPPED: { name: "Dropped", value: "DROPPED" },
PAUSED: { name: "Paused", value: "PAUSED" },
REPEATING: { name: "Rewatching", value: "REPEATING" },
};
setStatuses(statusMapping[gut.status]);
}
}
}
if (data.nextAiringEpisode) {
setTime(
convertSecondsToTime(data.nextAiringEpisode.timeUntilAiring)
);
}
}
} catch (error) {
console.error(error);
setTimeout(() => {
window.location.reload();
}, 1000);
} finally {
setLoading(false);
}
}
}
fetchData();
}, [id, info, session?.user?.name]);
// console.log();
function handleOpen() {
setOpen(true);
document.body.style.overflow = "hidden";
}
function handleClose() {
setOpen(false);
document.body.style.overflow = "auto";
}
return (
<>
{info
? info?.title?.romaji || info?.title?.english
: "Retrieving Data..."}
handleClose()}>
{!session && (
Edit your list
)}
{session && info && (
)}
{/* Mobile */}
{info?.title?.romaji || info?.title?.english}
{info?.genres
?.slice(
0,
info?.genres?.length > 3 ? info?.genres?.length : 3
)
.map((item, index) => (
{item}
))}
{info && (
)}
{info && info.status !== "NOT_YET_RELEASED" ? (
<>
{info?.type}
{info?.episodes ? (
{info?.episodes} Episodes
) : (
TBA
)}
>
) : (
{info && "Not Yet Released"}
)}
{/* PC */}
{info ? (
<>
>
) : (
)}
{/* PC */}
{info ? (
info?.title?.romaji || info?.title?.english
) : (
)}
{info ? (
{info?.episodes && (
{info?.episodes} Episodes
)}
{info?.startDate?.year && (
{info?.startDate?.year}
)}
{info?.averageScore && (
{info?.averageScore}%
)}
{info?.type && (
{info?.type}
)}
{info?.status && (
{info?.status}
)}
Sub | EN
) : (
)}
{info ? (
) : (
)}
{info?.relations?.edges?.length > 0 && (
Relations
)}
{info?.relations?.edges?.length > 3 && (
setShowAll(!showAll)}
>
{showAll ? "show less" : "show more"}
)}
{info?.relations?.edges ? (
info?.relations?.edges
.slice(0, showAll ? info?.relations?.edges.length : 3)
.map((r, index) => {
const rel = r.node;
return (
{r.relationType}
{rel.title.userPreferred || rel.title.romaji}
{rel.type}
);
})
) : (
<>
{[1, 2, 3].map((item) => (
))}
>
)}
{info && (
Episodes
)}
{info?.nextAiringEpisode && (
)}
0
? episode?.some((item) => item?.title === null)
? "pointer-events-none"
: "cursor-pointer"
: "pointer-events-none"
}
onClick={() => {
setEpiView("1");
localStorage.setItem("epiView", "1");
}}
>
0
? episode?.some((item) => item?.title === null)
? "pointer-events-none"
: "cursor-pointer"
: "pointer-events-none"
}
onClick={() => {
setEpiView("2");
localStorage.setItem("epiView", "2");
}}
>
0
? `cursor-pointer`
: "pointer-events-none"
}
onClick={() => {
setEpiView("3");
localStorage.setItem("epiView", "3");
}}
>
{!loading ? (
episode && (
{episode?.length !== 0 && episode ? (
{epiView === "1"
? episode?.map((epi, index) => {
const time = artStorage?.[epi?.id]?.time;
const duration =
artStorage?.[epi?.id]?.duration;
let prog = (time / duration) * 100;
if (prog > 90) prog = 100;
return (
Episode {epi?.number}
);
})
: ""}
{epiView === "2" &&
episode?.map((epi, index) => {
const time = artStorage?.[epi?.id]?.time;
const duration = artStorage?.[epi?.id]?.duration;
let prog = (time / duration) * 100;
if (prog > 90) prog = 100;
return (
{epi?.title}
{epi?.description && (
{epi?.description}
)}
);
})}
{epiView === "3" &&
episode?.map((epi, index) => {
return (
Episode {epi.number}
{epi.title && (
"{epi.title}"
)}
{index !== episode?.length - 1 && (
)}
);
})}
) : (
No Episodes Available
)}
)
) : (
)}
{info && rec?.length !== 0 && (
)}
>
);
}
export async function getServerSideProps(context) {
const { id } = context.query;
const res = await fetch("https://graphql.anilist.co/", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
query: GET_MEDIA_INFO,
variables: {
id: id?.[0],
},
}),
});
const json = await res.json();
const data = json?.data?.Media;
if (!data) {
return {
notFound: true,
};
}
const textColor = setTxtColor(data?.coverImage?.color);
const color = {
backgroundColor: `${data?.coverImage?.color || "#ffff"}`,
color: textColor,
};
return {
props: {
info: data,
color: color,
},
};
}
function convertSecondsToTime(sec) {
let days = Math.floor(sec / (3600 * 24));
let hours = Math.floor((sec % (3600 * 24)) / 3600);
let minutes = Math.floor((sec % 3600) / 60);
let time = "";
if (days > 0) {
time += `${days}d `;
}
if (hours > 0) {
time += `${hours}h `;
}
if (minutes > 0) {
time += `${minutes}m `;
}
return time.trim();
}
function getBrightness(hexColor) {
if (!hexColor) {
return 200;
}
const rgb = hexColor
.match(/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i)
.slice(1)
.map((x) => parseInt(x, 16));
return (299 * rgb[0] + 587 * rgb[1] + 114 * rgb[2]) / 1000;
}
function setTxtColor(hexColor) {
const brightness = getBrightness(hexColor);
return brightness < 150 ? "#fff" : "#000";
}